package com.itcncp.ucenter.web.interceptor;

import com.itcncp.common.util.PropertiesFileUtil;
import com.itcncp.ucenter.rpc.api.UserCustomerInfoService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 公共拦截器
 * Created by shuitcncp on 2017/3/25.
 */
public class UcenterWebInterceptor extends HandlerInterceptorAdapter {

    private static final Logger LOGGER = LoggerFactory.getLogger(UcenterWebInterceptor.class);

    private static  final  String[] NOTAPIS =
            new String[]{"login","getRegisterCode","verifyRegisterCode","getRegisterCode","getPasswordCode"
                         ,"getXianxing","register","resetPwd","updatePwd"};

    @Autowired
    private UserCustomerInfoService userCustomerInfoService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 过滤ajax
        if (null != request.getHeader("X-Requested-With") && "XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))) {
            return true;
        }
        // itcncp-ui静态资源配置信息
        String appName = PropertiesFileUtil.getInstance().get("app.name");
        String uiPath = PropertiesFileUtil.getInstance().get("itcncp.ui.path");
        request.setAttribute("appName", appName);
        request.setAttribute("uiPath", uiPath);


        //不需要处理的接口
        if(null != request.getServletPath()){
            for (String notapi: NOTAPIS ) {
                if(request.getServletPath().contains(notapi)){
                    return  true;
                }
            }
        }

        if(null != request.getHeader("deviceAppNo")){

            //判断用户登录信息
            String deviceAppNo = request.getHeader("deviceAppNo");
            String userId = request.getHeader("userId");
            String token = request.getHeader("token");

            if(StringUtils.isNotBlank(deviceAppNo)) {

                if(deviceAppNo.equals("8188")){
                    return true;
                }

                Boolean loginTokenExpired =  userCustomerInfoService.UserTokenExpired(new Long(userId),deviceAppNo, token);

                if(loginTokenExpired){
                    response.setStatus(500);
                    return true;
                }

            }
        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        super.afterCompletion(request, response, handler, ex);
    }

    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        super.afterConcurrentHandlingStarted(request, response, handler);
    }

}
